package com.xxx.mch.service.impl;

import com.xxx.mch.dao.DepartmentRewardMapper;
import com.xxx.mch.dao.PersonalRewardMapper;
import com.xxx.mch.domain.dto.PersonalAwardCoefficientDto;
import com.xxx.mch.domain.po.DepartmentReward;
import com.xxx.mch.domain.po.PersonalReward;
import com.xxx.mch.domain.po.User;
import com.xxx.mch.domain.request.PersonalAwardRequest;
import com.xxx.mch.service.AwardService;
import com.xxx.mch.service.TotalScoreService;
import java.math.BigDecimal;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020/9/7
 */
@Service
public class AwardServiceImpl implements AwardService
{
    @Resource
    private DepartmentRewardMapper rewardMapper;

    @Resource
    private PersonalRewardMapper personalRewardMapper;

    @Autowired
    private TotalScoreService totalScoreService;

    @Autowired
    private UserServiceImpl userServiceImpl;


    @Override
    public void saveAwardMode(PersonalAwardRequest awardModeRequest)
    {
        if (this.exist(awardModeRequest.getMonth()))
        {
            //更新
            awardModeRequest.getAwardList().forEach(personalAwardCoefficientDto -> {
                PersonalReward personalReward = new PersonalReward();
                BeanUtils.copyProperties(personalAwardCoefficientDto, personalReward);
                personalReward.setConfirm(0);
                personalReward.setTotalAward(awardModeRequest.getTotalAward());
                personalReward.setMonth(awardModeRequest.getMonth());
                personalRewardMapper.updateByPrimaryKeySelective(personalReward);
            });
        }
        else
        {
            //插入
            awardModeRequest.getAwardList().forEach(personalAwardCoefficientDto -> {
                PersonalReward personalReward = new PersonalReward();
                BeanUtils.copyProperties(personalAwardCoefficientDto, personalReward);
                personalReward.setConfirm(0);
                personalReward.setTotalAward(awardModeRequest.getTotalAward());
                personalReward.setMonth(awardModeRequest.getMonth());
                personalRewardMapper.insertSelective(personalReward);
            });
        }
    }


    @Override
    public PersonalAwardRequest queryAwardMode(String month)
    {
        List<PersonalReward> personalRewards = personalRewardMapper.select(new PersonalReward().setMonth(month));
        if (CollectionUtils.isEmpty(personalRewards))
        {
            List<User> userList = userServiceImpl.allUser();

            List<PersonalReward> personalRewardsList = new ArrayList<>();

            for (User user : userList)
            {
                PersonalReward reward = new PersonalReward();
                reward.setMonth(month);
                reward.setNickName(user.getNickName());
                reward.setUid(user.getUserCode());
                reward.setCoefficient(new BigDecimal(1));
                personalRewardsList.add(reward);
            }
            personalRewardMapper.insertList(personalRewardsList);
            personalRewards = personalRewardMapper.select(new PersonalReward().setMonth(month));
        }

        List<PersonalAwardCoefficientDto> dtos = personalRewards.stream().map(personalReward -> {
            PersonalAwardCoefficientDto dto = new PersonalAwardCoefficientDto();
            BeanUtils.copyProperties(personalReward, dto);
            return dto;
        }).collect(Collectors.toList());

        PersonalAwardRequest awardModeRequest = new PersonalAwardRequest();
        awardModeRequest.setTotalAward(personalRewards.get(0).getTotalAward());
        awardModeRequest.setAwardList(dtos);
        awardModeRequest.setMonth(personalRewards.get(0).getMonth());
        return awardModeRequest;
    }


    @Override
    public boolean exist(String month)
    {
        int count = rewardMapper.selectCount(new DepartmentReward().setMonth(month));
        if (count <= 0)
        {
            count = personalRewardMapper.selectCount(new PersonalReward().setMonth(month));
        }
        return count > 0;
    }


    @Override
    public boolean isConfirm(String month)
    {
        return this.isConfirmPersonal(month) || totalScoreService.isConfirm(month);
    }


    @Override
    public void confirm(String month, Integer type)
    {
        if (type == 1)
        {
            totalScoreService.confirm(month);
        }
        else if (type == 2)
        {
            this.confirmPersonal(month);
        }
    }


    private void confirmPersonal(String month)
    {
        Weekend<PersonalReward> weekend = new Weekend<>(PersonalReward.class);
        WeekendCriteria<PersonalReward, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(PersonalReward::getMonth, month);
        personalRewardMapper.updateByExampleSelective(new PersonalReward().setConfirm(1), weekend);
    }


    private boolean isConfirmPersonal(String month)
    {
        Weekend<PersonalReward> weekend = new Weekend<>(PersonalReward.class);
        WeekendCriteria<PersonalReward, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(PersonalReward::getMonth, month);
        return personalRewardMapper.selectCount(new PersonalReward().setMonth(month).setConfirm(1)) > 0;
    }

}
